#include  <func.h>
int recvn(int netfd, void *buf, int length){
    int total = 0;//已经recv的总字节数
    char *p = (char *)buf;//void * --> char *
    while(total < length){
        int ret = recv(netfd,p+total,length-total,0);
        if(ret == 0){
            return -1;
        }
        total += ret;
    }
    return 0;
}


int puts_server(int netfd){
    char filename[1024] = {0};
    int length;
    recvn(netfd,&length,sizeof(int));
    recvn(netfd,filename,length);
    int fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
    off_t filesize;
    recvn(netfd,&length,sizeof(int));
    recvn(netfd,&filesize,length);
    printf("filesize = %ld\n", filesize);
    ftruncate(fd,filesize);
    char *p = (char *)mmap(NULL,filesize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    recvn(netfd,p,filesize);
    munmap(p,filesize);
    close(fd);
    return 0;
}
